import Router from 'vue-router';
import Login from './pages/login';
import Interface from './pages/interface';
import UserList from './pages/userList';
import Roles from './pages/Permissions_roles';
import Index from './pages/index';
import Rights from './pages/Permissions_rights';
import GoodsList from './pages/Goods_goodsList';
import Params from './pages/Goods_params';
import Categories from './pages/Goods_categories';
import Orders from './pages/Order_orders';
import Reports from './pages/View_reports';
import Add from './pages/Goods_add';
let router = new Router({
    routes: [
        {
            path: '/',
            redirect: 'login'
        },
        {
            path: '/login',
            component: Login,
        },
        {
            path: '/interface',
            component: Interface,
            children: [
                {
                    path: '/interface',
                    redirect: 'index'
                },
                {
                    path: 'index',
                    component: Index
                },
                {
                    path: 'users',
                    component: UserList
                },
                {
                    path: 'roles',
                    component: Roles
                },
                {
                    path: 'rights',
                    component: Rights
                },
                {
                    path: 'goods',
                    component: GoodsList,
                    children: [

                    ]
                },
                {
                    //goods下的路由
                    path: 'goods/add',
                    component: Add
                },
                {
                    path: 'params',
                    component: Params
                },
                {
                    path: 'categories',
                    component: Categories
                },
                {
                    path: 'orders',
                    component: Orders
                },
                {
                    path: 'reports',
                    component: Reports
                }
            ]
        },
    ]
})
//全局路由守卫
router.beforeEach((to, from, next) => {
    let token = window.localStorage.getItem('token')
    if (to.path == '/interface') {
        if (token) {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }
})
// 解决重复点击路由路径报错问题
// Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: "/interface/users".
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
export default router